<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\page\Form;

class Cascader extends Component
{
    public function _init()
    {
        $this->component = 'cascader';



    }

    public function Form()
    {
        parent::Form();

        $cascader_type = $this->getFormParam('cascader.type');
        if ($cascader_type == 'area'){
            $this->form_column->options( BrandHelper::cityOptions( $this->getFormParam('cascader.area_level') ?: 3 ) );
        }
        return $this->form_column;
    }

    /**
     * 编辑/新增查询字段过滤
     */
    public function checkFormValue()
    {

        $cascader_type = $this->getFormParam('cascader.type');
        if ($cascader_type == 'area'){
            $cascader = $this->getFormParam('cascader');
            //省市区字段
            $province = $cascader['province'] ?? '';
            $city = $cascader['city'] ?? '';
            $area = $cascader['area'] ?? '';

            $value = [];
            if ($province) $value[] = (int) ($this->raws[$province] ?? 0);
            if ($city) $value[] = (int) ($this->raws[$city] ?? 0);
            if ($area) $value[] = (int) ($this->raws[$area] ?? 0);

            $this->value = $value;

        }

    }


    /**
     * table表格内容
     * @param $key
     * @param $value
     */
    public function checkTableValue($key, $value)
    {
        $value = parent::checkTableValue($key, $value);
        return BrandHelper::arrayFindInArray(BrandHelper::string2array($value),$this->getTableParam('options'));
    }






}